﻿// <copyright file="Led.xaml.cs" company="ALCPU">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Arthur Liberman</author>
// <email>Arthur_Liberman@hotmail.com</email>
// <date>04-05-2010</date>
// <summary>Contains the LED codebehind.</summary>

namespace Chess
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for Led.xaml
    /// </summary>
    public partial class Led : UserControl
    {
        /// <summary>
        /// The turn on Storyboard.
        /// </summary>
        private readonly Storyboard turnOnStoryboard;

        /// <summary>
        /// The turn off Storyboard.
        /// </summary>
        private readonly Storyboard turnOffStoryboard;

        /// <summary>
        /// The turn off Storyboard.
        /// </summary>
        private readonly Storyboard flashStoryboard;

        /// <summary>
        /// Indicates whether the LED is on or off.
        /// </summary>
        private bool on;

        /// <summary>
        /// Indicates whether the led is flashing.
        /// </summary>
        private bool flashing;

        /// <summary>
        /// Initializes a new instance of the <see cref="Led"/> class.
        /// </summary>
        public Led()
        {
            this.InitializeComponent();

            this.on = false;
            this.flashing = false;
            this.turnOnStoryboard = this.FindResource("TurnOn") as Storyboard;
            this.turnOffStoryboard = this.FindResource("TurnOff") as Storyboard;
            this.flashStoryboard = this.FindResource("Flash") as Storyboard;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Led"/> is on.
        /// </summary>
        /// <value><c>true</c> if on; otherwise, <c>false</c>.</value>
        public bool On
        {
            get
            {
                return this.on;
            }

            set
            {
                if (value == true && this.on == false)
                {
                    this.TurnOn();
                }
                else if (value == false && this.on == true)
                {
                    this.TurnOff();
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Led"/> is flashing.
        /// </summary>
        /// <value><c>true</c> if flashing; otherwise, <c>false</c>.</value>
        public bool Flashing
        {
            get { return this.flashing; }
            set { this.Flash(value); }
        }

        /// <summary>
        /// Turns the LED on.
        /// </summary>
        public void TurnOn()
        {
            this.on = true;
            this.turnOnStoryboard.Begin();    
        }

        /// <summary>
        /// Turns the LED off.
        /// </summary>
        public void TurnOff()
        {
            this.on = false;
            this.flashing = false;
            this.turnOffStoryboard.Begin();  
        }

        /// <summary>
        /// Flashes this LED.
        /// </summary>
        public void Flash()
        {
            this.Flash(true);
        }

        /// <summary>
        /// Flashes the specified LED.
        /// </summary>
        /// <param name="value">if set to <c>true</c> turn on flashing.</param>
        private void Flash(bool value)
        {
            if (value == true)
            {
                if (this.on == false)
                {
                    this.turnOnStoryboard.Completed += new EventHandler(this.TurnLedOnForFlash);
                    this.turnOnStoryboard.Begin();
                }
                else
                {
                    this.flashStoryboard.Begin();
                }

                this.on = true;
                this.flashing = true;
            }
            else
            {
                this.TurnOff();
            }
        }

        /// <summary>
        /// Turns the led on for flashing sequence to look correct.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TurnLedOnForFlash(object sender, EventArgs e)
        {
            this.turnOnStoryboard.Completed -= this.TurnLedOnForFlash;
            this.flashStoryboard.Begin();         
        }
    }
}